import { defineStore } from 'pinia';

export const useOrderStore = defineStore('order', {
  state: () => ({
    orderItems: [] as Array<{
      id: number;
      name: string;
      imgurl: string;
      num: number;
      price: number;
    }>,
  }),
  actions: {
    setOrderItems(items: Array<{
      id: number;
      name: string;
      imgurl: string;
      num: number;
      price: number;
    }>) {
      // 从 localStorage 中获取现有的 orderItems
      let existingOrderItems = JSON.parse(localStorage.getItem('orderItems') || '[]');

      // 将新项添加到现有的 orderItems 中
      this.orderItems = [...existingOrderItems, ...items];

      // 更新 localStorage
      localStorage.setItem('orderItems', JSON.stringify(this.orderItems));
    },
  },
});